package com.aiyige.location;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;

import com.aiyige.R;
import com.aiyige.location.model.RegionData;
import com.aiyige.page.selectregion.model.HeaderRegionItem;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.PreferenceUtil;
import com.alibaba.fastjson.JSON;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhuyakun on 2017/10/27.
 */

public class LocationUtil {
    public static final int MAX_HISTORY_REGION_NUM = 3;
    public static final int MAX_HOT_REGION_NUM = 6;
    public static final String PREF_KEY_HISTORY_REGION_PREFIX = "com.aiyige.PREF.PREF_KEY_HISTORY_REGION_PREFIX_";
    public static final String PREF_KEY_SELECT_REGION_PREFIX = "com.aiyige.PREF.PREF_KEY_SELECT_REGION_PREFIX_";

    public static List<HeaderRegionItem> getCurrentUserHistoryRegion() {
        List<HeaderRegionItem> historyRegionItemList = new LinkedList<>();
        try {
            String json = PreferenceUtil.getString(PREF_KEY_HISTORY_REGION_PREFIX + AccountUtil.getCurrentUser().getId(), "");
            HeaderRegionItem[] tempRegionItemArray = JSON.parseObject(json, HeaderRegionItem[].class);
            int upLimit = tempRegionItemArray.length > MAX_HISTORY_REGION_NUM ? MAX_HISTORY_REGION_NUM : tempRegionItemArray.length;
            for (int i = 0; i < upLimit; ++i) {
                historyRegionItemList.add(tempRegionItemArray[i]);
            }
        } catch (Exception e) {

        }
        return historyRegionItemList;
    }

    public static void setCurrentUserHistoryRegion(List<HeaderRegionItem> headerRegionItemList) {
        if (headerRegionItemList == null || headerRegionItemList.isEmpty()) {
            PreferenceUtil.putString(PREF_KEY_HISTORY_REGION_PREFIX + AccountUtil.getCurrentUser().getId(), "");
            return;
        } else {
            int upLimit = headerRegionItemList.size() > MAX_HISTORY_REGION_NUM ? MAX_HISTORY_REGION_NUM : headerRegionItemList.size();
            List<HeaderRegionItem> tempHeaderRegionItemList = new LinkedList<>();
            for (int i = 0; i < upLimit; ++i) {
                tempHeaderRegionItemList.add(headerRegionItemList.get(i));
            }
            PreferenceUtil.putString(PREF_KEY_HISTORY_REGION_PREFIX + AccountUtil.getCurrentUser().getId(), JSON.toJSONString(tempHeaderRegionItemList));
        }
    }

    public static void setCurrentUserSelectRegion(RegionData regionData) {
        if (regionData != null) {
            PreferenceUtil.putString(PREF_KEY_SELECT_REGION_PREFIX + AccountUtil.getCurrentUser().getId(), JSON.toJSONString(regionData));
        } else {
            PreferenceUtil.putString(PREF_KEY_SELECT_REGION_PREFIX + AccountUtil.getCurrentUser().getId(), "");
        }
    }

    public static RegionData getCurrentUserSelectRegion() {
        try {
            String json = PreferenceUtil.getString(PREF_KEY_SELECT_REGION_PREFIX + AccountUtil.getCurrentUser().getId(), "");
            return JSON.parseObject(json, RegionData.class);
        } catch (Exception e) {
            return null;
        }
    }

    public static void showLocationFailedDialog(Context context) {
        new AlertDialog.Builder(context)
                .setTitle(R.string.location_failed)
                .setMessage(R.string.please_connect_network_and_open_location_permission)
                .setPositiveButton(R.string.got_it, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .create()
                .show();
    }
}
